import { IBaseContentPage } from '@lib/IBaseIssuePage';
import { SSLib } from '@lib/SSLib';
import * as $ from 'jquery';
import './issuesPage.less';

export class MergeRequestsNew implements IBaseContentPage {
    constructor() {
        ;
    }

    public IsMatch(url: string) {
        return /merge_requests\/new\?/.test(url);
    }

    public Execute() {
        init();
    }
}

function init() {
    if (SSLib.GetQueryValue("merge_request%5Btarget_branch%5D") === "master") {
        attachRemark();
    }

}

function attachRemark() {
    let $textarea = $("#merge_request_description");

    if (!$("#merge_request_description").is(':visible')) {
        return;
    }

    let curContent = $textarea.val() as string;
    if (curContent.indexOf('<!-- UPDATE_LOG_START -->') >= 0) {
        return;
    }

    let appendStr = `<!-- UPDATE_LOG_START -->
**更新内容**

* 功能新增：某某某功能；
* 功能新增：XXX按钮用于MMM；
* 功能变更：
* 功能优化：
* 修复：某Bug议题id

<!-- UPDATE_LOG_END -->

`;
    $textarea.val(appendStr + curContent);
    $textarea[0].dispatchEvent(new CustomEvent('input'));

    if ($('.js-assignee-search .is-default').length > 0) {
        $('.qa-assign-to-me-link')[0].dispatchEvent(new CustomEvent('click'));
    }
}
